N = 15
a = [[0] * N for _ in range(N)]
vis = [[0] * N for _ in range(N)]
sum_val = 0
ans = 100000
d = [(1, 0), (0, -1), (0, 1), (-1, 0)]  # 4个方向

def dfs(x, y, c, s):
    global ans
    if 2 * s > sum_val:  # 剪枝
        return
    if 2 * s == sum_val:
        # 如果当前路径长度更短且包含左上角格子
        if c < ans and vis[0][0]:
            ans = c
        return

    vis[x][y] = 1
    for k in range(4):
        tx, ty = x + d[k][0], y + d[k][1]
        if 0 <= tx < n and 0 <= ty < m and not vis[tx][ty]:
            dfs(tx, ty, c + 1, s + a[x][y])
    vis[x][y] = 0

m, n = map(int, input().split())
for i in range(n):
     row = list(map(int, input().split()))
     for j in range(m):
         a[i][j] = row[j]
         sum_val += a[i][j]  # 求所有格子的和
# c: 当前路径中已经访问的格子数量
# s: 当前路径中所有格子的数值之和
dfs(0, 0, 0, 0)
print(0 if ans == 100000 else ans)